import time

setGlobalDuration(-1)

beginPythonInst(1)
soundfont(duration=1)
endPythonInst()

proc = startCsound()

time.sleep(1)

scl = scale('Cm')
rnd = loopseg(25,55)

mrk = markov(3)
mrk.mkStartRecord()

# generates and records 30 seconds of random pitches...
def randomPitches():
    pitch = scl[rnd.next(4)]
    mrk.mkRecord(pitch)
    dict = {'soundfont': {}}
    dict['soundfont']['midipitch'] = pitch
    sendEvent(1, dict, proc)

# and play them back in a markov chain
def markovPlayback():
    pitch = mrk.next()
    dict = {'soundfont': {}}
    dict['soundfont']['midipitch'] = pitch
    sendEvent(1, dict, proc)

pat = pattern(.125, randomPitches, [1])
pat.start()
pat.play()

time.sleep(30)

pat.play(False)

time.sleep(2)

mrk.mkStartPlayback()
pat2 = pattern(.125, markovPlayback, [1])
pat2.start()
pat2.play()


